package com.hhw.dao;

import java.util.ArrayList;
import java.util.List;

import com.hhw.entity.UserRegister;
import com.hhw.util.DBUtil;




public class UserRegisterDao extends BaseDao{
			public int addUser(UserRegister a){
			int count =0;
			try{
				connection = DBUtil.getConnection();
				preparedStatement=connection.prepareStatement("insert into user(user_name,user_password,user_sex,user_phone,user_add) values(?,?,?,?,?)");
				preparedStatement.setString(1, a.getUser_name());
				preparedStatement.setString(2,a.getUser_password());
				preparedStatement.setString(3,a.getUser_sex());
				preparedStatement.setString(4,a.getUser_phone());
				preparedStatement.setString(5,a.getUser_add());
				count=preparedStatement.executeUpdate();
				
			}catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					
			    }finally{
			    	closed();
			    }
			return count;

				}	
			
			
			public boolean isExist(UserRegister m){
				boolean flag = false;
				try {
					int count = 0;
					connection = DBUtil.getConnection();
					String sql = "select count(*) as count from user where  user_name=?";
					preparedStatement = connection.prepareStatement(sql);
					preparedStatement.setString(1, m.getUser_name());
					resultSet = preparedStatement.executeQuery();
					while (resultSet.next()) {
						count = resultSet.getInt("count");
					}
					if (count != 0) {
						flag = true;
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					closed();
				}
				return flag;
			}
			
			
			
			public List<UserRegister> UserRegister(){
				List<UserRegister> list = new ArrayList<>();
				try{
					connection = DBUtil.getConnection();
					preparedStatement=connection.prepareStatement("select * from user ");
					resultSet=preparedStatement.executeQuery();
					UserRegister m= null;
					while(resultSet.next()){
						m= new UserRegister();
						 m.setUser_name(resultSet.getString("user_name"));
						 m.setUser_password(resultSet.getString("user_password"));
						 m.setUser_sex(resultSet.getString("user_sex"));
						 m.setUser_phone("user_phone");
						 m.setUser_add(resultSet.getString("user_add"));
						 list.add(m);
					}
					return list;
					}catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						return null;
				    }
			}
			
			
			
			
			
			
			
			
			
			
}
